package org.diffshop.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.diffshop.product.entity.PmsAttrEntity;
import org.diffshop.product.service.PmsAttrService;
import org.diffshop.product.vo.BaseAttrs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.diffshop.common.utils.PageUtils;
import org.diffshop.common.utils.Query;

import org.diffshop.product.dao.ProductAttrValueDao;
import org.diffshop.product.entity.ProductAttrValueEntity;
import org.diffshop.product.service.ProductAttrValueService;
import org.springframework.transaction.annotation.Transactional;


@Service("productAttrValueService")
public class ProductAttrValueServiceImpl extends ServiceImpl<ProductAttrValueDao, ProductAttrValueEntity> implements ProductAttrValueService {
    @Autowired
    private PmsAttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ProductAttrValueEntity> page = this.page(
                new Query<ProductAttrValueEntity>().getPage(params),
                new QueryWrapper<ProductAttrValueEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 批量保存商品属性
     * @param spuId
     * @param baseAttrs
     */
    @Override
    public void saveProductAttrBatch(Long spuId, List<BaseAttrs> baseAttrs) {
        if (baseAttrs != null && baseAttrs.size() > 0){
            List<ProductAttrValueEntity> collect = baseAttrs.stream().map(baseAttr -> {
                ProductAttrValueEntity valueEntity = new ProductAttrValueEntity();
                valueEntity.setSpuId(spuId);
                valueEntity.setAttrId(baseAttr.getAttrId());
                PmsAttrEntity attr = attrService.getById(baseAttr.getAttrId());
                valueEntity.setAttrName(attr.getAttrName());
                valueEntity.setAttrValue(baseAttr.getAttrValues());
                valueEntity.setQuickShow(baseAttr.getShowDesc());
                return valueEntity;
            }).collect(Collectors.toList());
            this.saveBatch(collect);
        }
    }
    /**
     * 查询Spu的基本属性
     * @param spuId
     * @return
     */
    @Override
    public List<ProductAttrValueEntity> queryBaseAttrBySpuId(Long spuId) {
        List<ProductAttrValueEntity> entities = this.list(new LambdaQueryWrapper<ProductAttrValueEntity>().eq(ProductAttrValueEntity::getSpuId, spuId));

        return entities;

    }

    /**
     * 修改，根据spuId修改
     * @param spuId
     * @param attrValueEntities
     */
    @Transactional
    @Override
    public void update(Long spuId, List<ProductAttrValueEntity> attrValueEntities) {
        // 删除spu原来的基本属性
        baseMapper.delete(new LambdaQueryWrapper<ProductAttrValueEntity>().eq(ProductAttrValueEntity::getSpuId, spuId));

        // 批量新增
        List<ProductAttrValueEntity> collect = attrValueEntities.stream().map(attrValue -> {
            attrValue.setSpuId(spuId);
            return attrValue;
        }).collect(Collectors.toList());
        this.saveBatch(collect);
    }

}
